package sg.edu.nus.iss.club;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collection;
import java.util.Date;

public class Club implements Serializable{
	
	private int numMembers; //member index, always increase
	private int count; //count total
	private ArrayList <Member> members;
	private HashMap<String, Facility> facilities;
	private BookingRegister br;
	
	public Club () {
		numMembers = 0;
		count = 0;
		members = new ArrayList <Member>();
		facilities = new HashMap <String, Facility>();
		br = new BookingRegister();
		
	}
	public Member getMember (int memberNum){
		if (memberNum <= count && memberNum >0) {
		
			return members.get(memberNum-1);
		}else {
			return null;
		}
	}
	
	public ArrayList <Member> getMembers(){
		ArrayList <Member> cloneMembers = new ArrayList <Member>();
		for (int n =0; n < members.size(); n ++) {
			if (members.get(n)!=null)
			cloneMembers.add(members.get(n));
		}
		
		return cloneMembers;
	
	}
	
	public Member addMember (String surname, String firstname, String secondName){
		
		this.numMembers ++;
		Member m = new Member (surname, firstname, secondName, numMembers);
		members.add(m);
		this.count++;
		
		return m;
		
	}

	public void removeMember(int memberNum) {
		if (memberNum <= numMembers && memberNum >0) {
			if (members.get(memberNum-1) != null) {
				members.set(memberNum-1, null);
				count --;
			}
			//throw exception if there's no such member
		}
		//throw exception if there's no such member..
	}
	
	public Facility getFacility (String fname){
		if (fname != null) {
			return facilities.get(fname);
		}else {
			return null;
		}
		
	}
	public Facility [] getFacilities() {
		Collection <Facility> c = facilities.values();
		Facility [] farr = c.toArray(new Facility[c.size()]);
		Arrays.sort(farr);
		return farr;
	}
	
	public void addFacility ( Facility f) {
		if (f != null) {
			facilities.put(f.getName(), f);
		}
	}
	
	public void removeFacility (String fname) {
		if (fname != null) {
			if (facilities.get(fname) != null){
				facilities.remove(fname);
			}
			
		}
	}
	
	public void addBooking(int memberNum, String facilityName, Date startDate, Date endDate)throws BadBookingException{
		Member m = members.get(memberNum);
		Facility f = facilities.get(facilityName);
		br.addBooking(m, f, startDate, endDate);
	}
	
	public ArrayList <Booking> getBookings(String facilityName, Date startDate, Date endDate){
		Facility f = facilities.get(facilityName);
		ArrayList <Booking> result = br.getBooking(f, startDate, endDate);	
		
		return result;
		
	}
	

	public void showMembers() {
		ArrayList <Member> cloneMembers = new ArrayList<Member>();
		cloneMembers = getMembers();
		
		for (int n = 0; n < cloneMembers.size(); n ++) {
			Member m = cloneMembers.get(n);
			m.show();
		}
	}
	
	public void showFacilities(){
		Facility [] farr = getFacilities();
		for (int n = 0 ; n < farr.length ; n++) {
			farr[n].show();
		}
	}
	
	public void showBookings(String facilityName, Date startDate, Date endDate){
		System.out.println("***********Booking List********");
		Facility f = facilities.get(facilityName);
		ArrayList <Booking> result = br.getBooking(f, startDate, endDate);	
		if (result != null && result.size() >0){
			for (Booking bk :  result){
				bk.show();
			}
		}
		
		
	}
	
	public void show(){
		System.out.println ("************ Member List **************");
		showMembers();
		System.out.println ("************ Facility List **************");
		showFacilities();
	}
		
}
	
